package com.itheima.server.controller;

import com.itheima.domain.db.Settings;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.dubbo.api.FriendApi;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.server.service.UserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author 阿鹏
 * @date 2021/1/24 21:10
 */
@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping
    public ResponseEntity findUserInfo(@RequestParam(required = false) Long userId,
                                       @RequestParam(required = false) Long huanxinID) {
        Long id = UserHolder.getUserId();
        if (userId != null) {
            id = userId;
        }
        if (huanxinID != null) {
            id = huanxinID;
        }
        return userInfoService.findUserInfo(id);
    }

    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        return userInfoService.updateUserInfo(userInfo);
    }

    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) throws IOException {
        return userInfoService.updateHeader(headPhoto);
    }

    @GetMapping("/settings")
    public ResponseEntity getSettings() {
        return userInfoService.getSettings();
    }

    @GetMapping("/blacklist")
    public ResponseEntity getBlackList(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pageSize) {
        return userInfoService.getBlackList(page, pageSize);
    }

    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity removeFromBlacklist(@PathVariable("uid") Long blackUserId) {
        return userInfoService.removeFromBlacklist(blackUserId);
    }

    @PostMapping("/questions")
    public ResponseEntity saveQuestions(@RequestBody Map map) {
        String content = (String) map.get("content");
        return userInfoService.saveQuestions(content);
    }

    @PostMapping("/notifications/setting")
    public ResponseEntity saveSettings(@RequestBody Settings settings) {
        return userInfoService.saveSettings(settings);
    }

    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        return userInfoService.sendVerificationCode();
    }

    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String verificationCode = (String) map.get("verificationCode");
        return userInfoService.checkVerificationCode(verificationCode);
    }

    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        return userInfoService.savePhone(phone);
    }

    @GetMapping("counts")
    public ResponseEntity findLikeLoveFriendCounts() {
        return userInfoService.findLikeLoveFriendCounts();
    }

    @GetMapping("/friends/{type}")
    public ResponseEntity findUserLikeList(@PathVariable("type") Integer type,
                                           @RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "10") int pagesize) {
        return userInfoService.findUserLikeList(type, page, pagesize);
    }

    @PostMapping("/fans/{fansId}")
    public ResponseEntity likeFans(@PathVariable("fansId") Long fansId){
        return userInfoService.likeFans(fansId);
    }


    @DeleteMapping("like/{userId}")
    public ResponseEntity removeLike(@PathVariable("userId") Long targetUserId) {

        return userInfoService.removeLike(targetUserId);
    }
}
